import {ApiClient} from './api'
import {createMessageConfig} from '@/utils/responseHandler'
import type {CreateFieldLibraryRequest, FieldLibrary, FieldLibraryPageResponse, FieldLibrarySearchRequest, UpdateFieldLibraryRequest} from '@/types/business/fieldLibrary'

export default {
  /**
   * 创建字段库记录
   */
  createField(data: CreateFieldLibraryRequest) {
    return ApiClient.post<FieldLibrary>('/metadata/field-library', data, createMessageConfig('字段创建成功'))
  },

  /**
   * 更新字段库记录
   */
  updateField(id: number, data: UpdateFieldLibraryRequest) {
    return ApiClient.put<FieldLibrary>(`/metadata/field-library/${id}`, data, createMessageConfig('字段更新成功'))
  },

  /**
   * 删除字段库记录
   */
  deleteField(id: number) {
    return ApiClient.delete<void>(`/metadata/field-library/${id}`, createMessageConfig('字段删除成功'))
  },

  /**
   * 搜索字段库记录
   */
  searchFields(data: FieldLibrarySearchRequest) {
    return ApiClient.post<FieldLibraryPageResponse>('/metadata/field-library/search', data, { showSuccess: false })
  },

  /**
   * 获取产品所有字段
   */
  getAllFieldsByProduct(productId: number) {
    return ApiClient.get<FieldLibrary[]>(`/metadata/field-library/product/${productId}`, { showSuccess: false })
  },

  /**
   * 翻译字段名称
   */
  translateFieldName(chineseName: string) {
    return ApiClient.get<string[]>('/metadata/field-library/translate', {
      params: { chineseName },
      showSuccess: false
    })
  }
}
